package com.liu.controller;

import com.alibaba.fastjson.JSONObject;
import com.liu.pojo.*;
import com.liu.service.ManageService;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RequestMapping("/manager")
@RestController
@Slf4j
public class ManagerController {
    @Autowired
    private ManageService manageService;

    //管理员登录
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody Manager manager){
        Boolean isTrue = manageService.login(manager);
        if(isTrue){
            HashMap<String, Object> claims = new HashMap<>();
            claims.put("manageId",manager.getManagerId());
            claims.put("password",manager.getPassword());
            String jwt = Jwts.builder()
                    .signWith(SignatureAlgorithm.HS256,"liuhaha")
                    .setClaims(claims)
                    .setExpiration(new Date(System.currentTimeMillis() + 3600*1000))
                    .compact();
//            return Result.success(jwt);
            //设置HTTP响应码
            return new ResponseEntity<>(JSONObject.toJSONString(Result.success(jwt)), HttpStatus.OK);
        }
//        return Result.error("登录失败");
        return new ResponseEntity<>(JSONObject.toJSONString(Result.error("登录失败")), HttpStatus.BAD_REQUEST);
    }

    //管理员上传电影
    @PostMapping("/video")
    public ResponseEntity<String> uploadVideo(@RequestBody MultipartFile img, @RequestBody MultipartFile video,
                                              String fileName,
                                              String director,
                                              String leadingActors,
                                              String description,
                                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate releaseTime,
                                              String labels) {
        log.info("电影信息如下：");
        log.info("电影名：{}",fileName);
        log.info("导演：{}",director);
        log.info("主演列表：{}",leadingActors);
        log.info("电影描述：{}",description);
        log.info("上映日期：{}",releaseTime);
        log.info("标签：{}",labels);
        Video videoObj = new Video(img, video,fileName, director, leadingActors, description, releaseTime, labels,null);
        Boolean isTrue = manageService.upload(videoObj);
        if(isTrue){
            return new ResponseEntity<>(JSONObject.toJSONString(Result.success()),HttpStatus.OK);
        }
        return new ResponseEntity<>(JSONObject.toJSONString(Result.error("crash")),HttpStatus.INTERNAL_SERVER_ERROR);
    }

    //查询电影列表
    @PostMapping("/getVideo")
    public ResponseEntity<String> getVideos(@RequestBody PageBean pageBean){
        List<VideoUrl> videoUrls = manageService.getVideo(pageBean);
        if(!videoUrls.isEmpty()){
            return new ResponseEntity<>(JSONObject.toJSONString(Result.success(videoUrls)),HttpStatus.OK);
        }
        return new ResponseEntity<>(JSONObject.toJSONString(Result.error("NO_FIND")),HttpStatus.NOT_FOUND);
    }

    //删除电影
    @DeleteMapping("/video/{id}")
    public ResponseEntity<String> deleteVideo(@PathVariable Integer id){
        Boolean isTrue = manageService.delete(id);
        if(isTrue){
            return new ResponseEntity<>(JSONObject.toJSONString(Result.success()),HttpStatus.OK);
        }
        return new ResponseEntity<>(JSONObject.toJSONString(Result.error("DELETE_FAIL")),HttpStatus.INTERNAL_SERVER_ERROR);

    }

    //获取电影详细信息
    @GetMapping("/video/{id}")
    public ResponseEntity<String> getVideoById(@PathVariable Integer id){
        //对id号不存在的异常处理
        VideoUrl videoUrl = null;
        videoUrl = manageService.getVideoById(id);
        return new ResponseEntity<>(JSONObject.toJSONString(Result.success(videoUrl)),HttpStatus.OK);
    }

    //修改电影信息
    @PutMapping("/video")
    public ResponseEntity<String> updateVideo(@RequestBody MultipartFile img, @RequestBody MultipartFile video,
                                              String fileName,
                                              String director,
                                              String leadingActors,
                                              String description,
                                              @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate releaseTime,
                                              String labels,
                                              Integer id){
        Video videoObj = new Video(img, video,fileName, director, leadingActors, description, releaseTime, labels,id);
        manageService.updateVideo(videoObj);
        //异常先不做处理
        return new ResponseEntity<>(JSONObject.toJSONString(Result.success()),HttpStatus.OK);

    }
}
